/**

 * 2010(c) Copyright Oceansoft Information System Co.,LTD. All rights reserved.
 * <p>
 * Compile: JDK 1.6+
 * <p>
 * 版权所有(C)：江苏欧索软件有限公司
 * <p>
 * 公司名称：江苏欧索软件有限公司
 * <p>
 * 公司地址：中国苏州科技城青山路1号
 * <p>
 * 网址: http://www.oceansoft.com.cn
 * <p>
 * 作者: 090922(陈伟)
 * <p>
 * 文件名: com.oceansoft.mobile.biz.front.controller.ChannelController.java
 * <p>
 * 类产生时间: 2014/3/27 21:12
 * <p>
 * 负责人: 090922(陈伟)
 * <p>
 * Email:javacspring@gmail.com
 * <p>
 * 所在组 : 掌上公安应用平台
 * <p>
 * 所在部门: 开发部--手持技术部
 * <p>
 * <p>
 */
package com.oceansoft.mobile.biz.admin.controller;

import static org.springframework.web.bind.annotation.RequestMethod.GET;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.oceansoft.mobile.biz.admin.entity.Wxjq;
import com.oceansoft.mobile.biz.admin.service.IWxjqService;
import com.oceansoft.mobile.common.base.entity.Pager;
import com.oceansoft.mobile.common.base.entity.Query;

/**
 * 微信集群控制器
 * 
 * @author yixin
 * 
 */
@Controller
@RequestMapping("admin/wxjq")
public class WxjqAdminController {
	@Resource
	private IWxjqService wxjqService;
	
	@Value("#{propertiesReader[icons_path]}")
	private String iconsPath;

	/**
	 * 打开微信集群列表
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "index", method = GET)
	public String gotoChannelList(Model model) {
		return "admin/wxjq/wxjq-list";
	}

	/**
	 * 获取微信集群数据
	 * @param session
	 * @param query
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "newslist", method = GET)
	public Pager<Wxjq> gotoNewsList(HttpSession session, Query query) {
//		SysUser user = (SysUser) session.getAttribute(Constant.GLOBAL_SESSION);
		String title="";
		Pager<Wxjq> pager = wxjqService.loadWxjqByPage(title,query);
		return pager;
	}
	
	/**
	 * 根据图标文件名获取图片
	 * @param iconname
	 * @param request
	 * @param response
	 */
	@RequestMapping(value = "iconpic/{iconname}", method = GET)
	public void getIconPic(@PathVariable String iconname,
			HttpServletRequest request, HttpServletResponse response) {
		response.setContentType("image/jpeg"); // 设置返回内容格式
		
		String filePath="";
		if(iconname.indexOf(".png")>0){
			filePath = new StringBuffer(iconsPath).append("/").append(iconname).toString();
		}else{
			filePath = new StringBuffer(iconsPath).append("/").append(iconname).append(".png").toString();
		}
		File file = new File(filePath); // 括号里参数为文件图片路径
		if (file.exists()) { // 如果文件存在
			BufferedInputStream bis = null;
			BufferedOutputStream bos = null;
			try {
				bis = new BufferedInputStream(new FileInputStream(file.getAbsoluteFile()));  
				bos = new BufferedOutputStream(response.getOutputStream()); // 创建输出流
				byte[] b = new byte[1024];
				while (bis.read(b) != -1) {
					bos.write(b);
				}
				bis.close();
				bos.flush();
				bos.close();
			} catch (Exception e) {
				e.printStackTrace();
			} finally{
				if(null != bis){
					try {
						bis.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
				if(null != bos){
					try {
						bos.flush();
						bos.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
			}
		}
	}
	
//	/**
//	 * 进入编辑或者增加的页面
//	 * 
//	 * @param session
//	 * @param sid
//	 * @param model
//	 * @return
//	 */
//	@RequestMapping(value = "gotoAdd", method = GET)
//	public String gotoArticleEdit(HttpSession session, @RequestParam(value = "sid", required = false) long sid, Model model) {
//		if (sid > 0) {// 编辑
//			NewsArticle art = newsService.loadNewsById(sid);
//			art.setContent(HtmlUtils.htmlEscape(art.getContent()));
//			model.addAttribute("pic", art.getOriginalName());
//			model.addAttribute("news", art);
//			model.addAttribute("isAddNew", 0);
//		} else {
//			model.addAttribute("news", new NewsArticle());
//			model.addAttribute("isAddNew", 1);
//		}
//		List<NewsArticle> newArticles = new ArrayList<NewsArticle>();
//		newArticles = newsService.findTopNews();
//		Map<Integer, String> types = new HashMap<Integer, String>();
//		types.put(0, "头条新闻");
//		types.put(1, "封面新闻");
//		model.addAttribute("types", types);
//		model.addAttribute("newArticlesList", newArticles);
//		return "admin/police/news-add";
//	}
	
	public String getIconsPath() {
		return iconsPath;
	}

	public void setIconsPath(String iconsPath) {
		this.iconsPath = iconsPath;
	}

}
